package com.zqy.supernet.demo;


import com.trello.rxlifecycle4.components.RxActivity;
import com.trello.rxlifecycle4.components.support.RxAppCompatActivity;
import com.trello.rxlifecycle4.components.support.RxFragment;
import com.zqy.supernet.SuperNetManager;
import com.zqy.supernet.demo.bean.DemoBean;
import com.zqy.supernet.help.observer.EntityObserver;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.rxjava3.core.Observer;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

import static com.zqy.supernet.help.RxHelper.observableIO2Main;


/**
 * 提交参数方式
 */
public class RequestUtils {
    private static ApiUrl apiUrl;

    private static ApiUrl getApiUrl() {
        if (apiUrl == null) {
            apiUrl = SuperNetManager.getApiService(ApiUrl.class,Constans.BaseUrl);
        }
        return apiUrl;
    }

    /**
     * Get 请求demo
     *
     * @param context
     * @param observer
     */
    public static void getDemo(RxActivity context, EntityObserver<DemoBean> observer) {
        getApiUrl().getDemo()
                .compose(observableIO2Main(context))
                .subscribe(observer);
    }

    /**
     * Get 请求demo
     *
     * @param context
     * @param observer
     */
    public static void getDemoList(RxActivity context, EntityObserver<List<DemoBean>> observer) {
        getApiUrl()
                .getDemoList().compose(observableIO2Main(context))
                .subscribe(observer);
    }

    /**
     * Post 请求demo
     *
     * @param context
     * @param consumer
     */
    public static void postDemo(RxAppCompatActivity context, String name, String password, Observer<DemoBean> consumer) {
        getApiUrl()
                .postUser(name, password).compose(observableIO2Main(context))
                .subscribe(consumer);
    }

    /**
     * Put 请求demo
     *
     * @param context
     * @param consumer
     */
    public static void putDemo(RxFragment context, String access_token, Observer<DemoBean> consumer) {
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Accept", "application/json");
        headers.put("Authorization", access_token);
        getApiUrl()
                .put(headers, "厦门")
                .compose(observableIO2Main(context))
                .subscribe(consumer);
    }

    /**
     * Delete 请求demo
     *
     * @param context
     * @param consumer
     */
    public static void deleteDemo(RxFragment context, String access_token, Observer<DemoBean> consumer) {
        getApiUrl()
                .delete(access_token, 1)
                .compose(observableIO2Main(context))
                .subscribe(consumer);
    }

    /**
     * 上传图片
     *
     * @param context
     * @param observer
     */
    public static void upImagView(RxFragment context, String access_token, String str, Observer<DemoBean> observer) {
        File file = new File(str);
//        File file = new File(imgPath);
        Map<String, String> header = new HashMap<String, String>();
        header.put("Accept", "application/json");
        header.put("Authorization", access_token);
//        File file =new File(filePath);
        RequestBody reqFile = RequestBody.create(MediaType.parse("image/*"), file);
//        RequestBody requestFile =
//                RequestBody.create(MediaType.parse("multipart/form-data"), file);
        MultipartBody.Part body =
                MultipartBody.Part.createFormData("file", file.getName(), reqFile);
        getApiUrl().uploadImage(header, body).compose(observableIO2Main(context))
                .subscribe(observer);


    }

    /**
     * 上传多张图片
     *
     * @param files
     */
    public static void upLoadImg(RxFragment context, String access_token, List<File> files, Observer<DemoBean> observer1) {
        Map<String, String> header = new HashMap<String, String>();
        header.put("Accept", "application/json");
        header.put("Authorization", access_token);
        MultipartBody.Builder builder = new MultipartBody.Builder()
                .setType(MultipartBody.FORM);//表单类型
        for (int i = 0; i < files.size(); i++) {
            File file = files.get(i);
            RequestBody photoRequestBody = RequestBody.create(MediaType.parse("image/*"), file);
            builder.addFormDataPart("file", file.getName(), photoRequestBody);
        }
        List<MultipartBody.Part> parts = builder.build().parts();
        getApiUrl().uploadImage1(header, parts).compose(observableIO2Main(context))
                .subscribe(observer1);
    }
}

